$(function(){
	_user = getCurrLoginUser();
	if(_user.type!="0"){
		$("#add").remove();
	}
	customTable = new Custom_table();
	
	/*initUserType();
	initUserStatus();*/
	
	initPageTable();
})

function initUserType(){
	initCodeConfigComponent("#type_name","#type",{"codeType":"userType"},null);
}

function initUserStatus(){
	initCodeConfigComponent("#status_name","#status",{"codeType":"userStatus"},null);
}

function addUser(){
	art.dialog.open("addUser.html",{
			"title":"新增用户",
			"width":"800px",
			"height":"350px"
		},false);
}

//1、查询数据
function queryListData(){
	customTable.initTable();
}

function initPageTable(){
	var mainform = $('#mainform');
	customTable.init({
		table : mainform.find('#listDataTable'),
		tableTbody : mainform.find('#listDataTableTbody'),
		tablePageDiv : mainform.find('#listDataTablePageDiv'),
		columnNum : 11,
		processRowFun : processListDataRow,
		initQueryDataFun : initQueryData
	});	
}

//2、实现：初始化条件数据
function initQueryData() {
	var requestObj = new Object();
	requestObj['url'] = "/ControlServlet/doPost.action";
	requestObj['data'] = new Object();
	requestObj['data']['pageSize'] = customTable.getPageSize();
	requestObj['data']['startNum'] = customTable.getStartNum();
	//定义查询条件
	requestObj['data']['serviceName'] = "userServlet";
	requestObj['data']['methodName'] = "getUserList";
	requestObj['data']['USER_NAME'] = processUndefined($("#user_name").val());
	requestObj['data']['STATUS_CD'] = processUndefined($("#status").val());
	requestObj['data']['REAL_NAME'] = processUndefined($("#real_name").val());
	requestObj['data']['TYPE'] = processUndefined($("#type").val());
	return requestObj;
}

//3、实现：处理行数据方法
function processListDataRow( seq , tempBean) {
	var rowHtml = [];
	rowHtml.push('<td align="center">' + processUndefined(tempBean.USER_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.REAL_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.TYPE_NAME) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.MOBILE_NO) + '&nbsp;</td>');
	rowHtml.push('<td align="center">' + processUndefined(tempBean.ID_CARD_NO) + '&nbsp;</td>');
	rowHtml.push('<td align="center">'+ processUndefined(tempBean.STATUS_NAME) +'&nbsp;</td>');
	rowHtml.push('<td align="center">'+ initHandler(tempBean)+'&nbsp;</td>');
	return rowHtml.join("");
}

//操作权限控制
function initHandler(tempBean){
	var rtnHtml = [];
	if(_user){
		//管理员
		if("0"==_user.type){
			if("1001"==processUndefined(tempBean.STATUS_CD)){
				rtnHtml.push('<a onclick="unFreezeUser(\''+processUndefined(tempBean.USER_NAME)+'\','+processUndefined(tempBean.USER_ID)+')" class="" >解冻</a>&nbsp;');
			}
			if("1000"==processUndefined(tempBean.STATUS_CD)){
				rtnHtml.push('<a onclick="freezeUser(\''+processUndefined(tempBean.USER_NAME)+'\','+processUndefined(tempBean.USER_ID)+')" class="" >冻结</a>&nbsp;');
			}
			rtnHtml.push('<a onclick="modifyUser('+processUndefined(tempBean.USER_ID)+')" class="" >修改</a>&nbsp;');
//			rtnHtml.push('<a onclick="delProc('+processUndefined(tempBean.PRO_ID)+','+processUndefined(tempBean.PROD_ID)+','+processUndefined(tempBean.USER_ID)+')" class="" >删除</a>');
		} else{//非管理员
			rtnHtml.push('<a onclick="modifyUser('+processUndefined(tempBean.USER_ID)+')" class="" >修改</a>&nbsp;');
		}
	}
	return rtnHtml.join("");
}

function modifyUser(user_id){
	var param = "?user_id="+user_id;
	art.dialog.open("modifyUser.html"+param,{
			"title":"修改用户信息",
			"width":"800px",
			"height":"350px"
		},false);
}


function unFreezeUser(user_name,user_id){
	art.dialog.isSubmit("是否解冻此员工账号"+user_name+"？",function(){
		var data = {
				"STATUS_CD":"1000",
				"USER_ID":user_id,
				"OPERTYPE":"unfreeze"
		}
		$.jsonAjax("userServlet","updateUserInfo",data,null,false,function(rtnData){
			if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
				art.dialog.message("succeed","账号"+user_name+"已解冻",function(){
					queryListData();
				});
			} else{
				art.dialog.message("error","账号"+user_name+"解冻失败！",null);
			}
		});
	},null);
}

function freezeUser(user_name,user_id){
	art.dialog.isSubmit("是否冻结此员工账号"+user_name+"？",function(){
		var data = {
				"STATUS_CD":"1001",
				"USER_ID":user_id,
				"OPERTYPE":"freeze"
		}
		$.jsonAjax("userServlet","updateUserInfo",data,null,false,function(rtnData){
			if(!isEmpty(rtnData)&&rtnData.data.status=="success"){
				art.dialog.message("succeed","账号"+user_name+"已冻结",function(){
					queryListData();
				});
			} else{
				art.dialog.message("error","账号"+user_name+"冻结失败！",null);
			}
		});
	},null);
}
